/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TelaConsultaAluno.java
 *
 * Created on 23/06/2011, 20:39:34
 */
package telas;

import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
import pacoteDao.*;
import projetofinal.*;

/**
 *
 * @author Iana
 */
public class TelaConsultaAluno extends javax.swing.JFrame {

    /** Creates new form TelaConsultaAluno */
    public TelaConsultaAluno() {
        initComponents();
        jTableResult.getColumnModel().getColumn(0).setHeaderValue("NOME");
        jTableResult.getColumnModel().getColumn(1).setHeaderValue("CPF");
        jTableResult.getColumnModel().getColumn(2).setHeaderValue("RG");
        jTableResult.getColumnModel().getColumn(3).setHeaderValue("IDADE");
        jTableResult.getColumnModel().getColumn(4).setHeaderValue("TELEFONE");
        jTableResult.getColumnModel().getColumn(5).setHeaderValue("DATA NASC");
        jTableResult.getColumnModel().getColumn(6).setHeaderValue("SERIE");
        jTableResult.getTableHeader().resizeAndRepaint();
        ((DefaultTableModel) jTableResult.getModel()).setNumRows(0);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextFieldConsulta = new javax.swing.JTextField();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jScrollPaneResult = new javax.swing.JScrollPane();
        jTableResult = new javax.swing.JTable();
        jLabel3 = new javax.swing.JLabel();
        jButtonConsultar = new javax.swing.JButton();
        jButtonRetornar = new javax.swing.JButton();
        jButtonSair = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel1.setText("CONSULTA DE ALUNOS");

        jTextFieldConsulta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldConsultaActionPerformed(evt);
            }
        });

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Todos", "Nome" }));

        jLabel2.setText("Consulta Aluno por:");

        jTableResult.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4", "Título 5", "Título 6", "Título 7"
            }
        ));
        jScrollPaneResult.setViewportView(jTableResult);

        jLabel3.setText("Consultar:");

        jButtonConsultar.setText("Consultar");
        jButtonConsultar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonConsultarActionPerformed(evt);
            }
        });

        jButtonRetornar.setText("Retornar");
        jButtonRetornar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRetornarActionPerformed(evt);
            }
        });

        jButtonSair.setText("Sair");
        jButtonSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSairActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(177, Short.MAX_VALUE)
                .addComponent(jButtonConsultar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonRetornar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonSair)
                .addGap(212, 212, 212))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextFieldConsulta)))
                .addContainerGap(361, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(197, 197, 197)
                .addComponent(jLabel1)
                .addContainerGap(219, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPaneResult, javax.swing.GroupLayout.DEFAULT_SIZE, 586, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 9, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextFieldConsulta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jScrollPaneResult, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonConsultar)
                    .addComponent(jButtonRetornar)
                    .addComponent(jButtonSair))
                .addGap(27, 27, 27))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonConsultarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonConsultarActionPerformed
        // TODO add your handling code here:
        ((DefaultTableModel) jTableResult.getModel()).setNumRows(0);
        jTableResult.updateUI();
        Aluno aluno = new Aluno();
        try {
            AlunoDAO alunoDao = new AlunoDAO();
            List<Aluno> listaAlunos = null;
            if (((String) jComboBox1.getSelectedItem()).equals("Todos")) {
                listaAlunos = alunoDao.consultar();
            } else {
                Aluno alunos = alunoDao.consultaNome(jTextFieldConsulta.getText());
                listaAlunos.add(alunos);
            }

            jTableResult.setEnabled(true);
            int linha = 0;
            int coluna = 0;
            ((DefaultTableModel) jTableResult.getModel()).setNumRows(listaAlunos.size());
            for (Aluno alunos : listaAlunos) {
                jTableResult.setValueAt(alunos.getNome(), linha, coluna);//valor//linha//coluna
                coluna++;
                jTableResult.setValueAt(alunos.getCpf(), linha, coluna);//valor//linha//coluna
                coluna++;
                jTableResult.setValueAt(alunos.getRg(), linha, coluna);//valor//linha//coluna
                coluna++;
                jTableResult.setValueAt(alunos.getIdade(), linha, coluna);//valor//linha//coluna
                coluna++;
                jTableResult.setValueAt(alunos.getTelefone(), linha, coluna);//valor//linha//coluna
                coluna++;
                jTableResult.setValueAt(alunos.getDtnasc(), linha, coluna);//valor//linha//coluna
                coluna++;
                jTableResult.setValueAt(alunos.getIdserie(), linha, coluna);//valor//linha//coluna
                linha++;
                coluna = 0;
            }
        } catch (SQLException ex) {
            Logger.getLogger(TelaConsultaAluno.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_jButtonConsultarActionPerformed

    private void jButtonRetornarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRetornarActionPerformed
        // TODO add your handling code here:
        TelaPrincipal frame = new TelaPrincipal();
        this.setVisible(false);
        frame.setVisible(true);
    }//GEN-LAST:event_jButtonRetornarActionPerformed

    private void jButtonSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSairActionPerformed
        // TODO add your handling code here:
        System.exit(0);
    }//GEN-LAST:event_jButtonSairActionPerformed

    private void jTextFieldConsultaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldConsultaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextFieldConsultaActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new TelaConsultaAluno().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonConsultar;
    private javax.swing.JButton jButtonRetornar;
    private javax.swing.JButton jButtonSair;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPaneResult;
    private javax.swing.JTable jTableResult;
    private javax.swing.JTextField jTextFieldConsulta;
    // End of variables declaration//GEN-END:variables
}
